var host = window.location.protocol + '//' + window.location.host;
var url = {
	sendcode: 'http://www.niuniumenke.com/api/send_sms',   // 发送短信验证码
	sendmail: 'http://www.niuniumenke.com/api/send_email',   // 发送邮件
	regshuju: 'http://www.niuniumenke.com/api/register',   //注册
	captcha: 'http://www.niuniumenke.com/api/captcha',   //注册
	// areaCodes: 'http://www.niuniumenke.com/api/country/codes',   //注册
	content: 'http://www.niuniumenke.com/api/app/download',   //注册
};
$('#captcha').hide()
//tjuser
var Numbererweima = GetQueryString("invite_code");
$('#tjuser').val(Numbererweima);
if (!Numbererweima) {
	layer.open({
		style: 'border:none; background-color:#404040; color:#fff;',
		content: '邀请码错误',
		time: 0,
		shadeClose: false
	});
}

// $.get(url.areaCodes, {
// }, function (data) {
//   console.log(data)
// 	if (data.status_code == "200") {
// 		let dd ='';
// 		for(var i =0;i<data.data.length;i++){
// 			dd+='<li data-mcode="'+data.data[i].code+'">'
// 			+'<span>'+data.data[i].country+'</span>'
// 			+'<span>'+data.data[i].code+'</span>'
// 			+'</li>'
// 		}
// 		$('#dropDown').html(dd)
// 	} else {
// 		LayerOpen(data.message);
// 	}
// })

var contentURL;

$.get(url.content, {
	// key:'software_link '
}, function (data) {
	if (data.status_code == "200") {
		contentURL  = data.data.link
	} else {
		LayerOpen(data.message);
	}
})


var CaptchaStatus = false;
var mcode = '86';
var registerType = 'mobile';
var Verification = $('#code'),
	register = $('#submit'),
	PhoneNumber = $('input[name="phone"]'),
	mobicode = $('input[name="code"]'),
	PassWord = $('input[name="pwd"]'),
	ConfirmPW = $('input[name="pwd1"]')
	Captcha = $('input[name="captcha"]')


Verification.click(function () {
	getCode();
});

function getCode() {
	if (registerType == "mobile") {
		if (!PhoneNumber.val()) {
			LayerOpen('请输入正确的手机号');
			return false;
		}
		if (!CaptchaStatus) {
			$.get(url.captcha, {
				key_str: PhoneNumber.val(),
			}, function (data) {
				if (data.status_code == "200") {
					LayerOpen('请填写图片验证码');
					$('#captcha img').attr('src',data.data.captcha_image_content);
					$('#captcha').show();
					CaptchaStatus = true;
				} else {
					LayerOpen(data.message);
				}
			})
			return
		}
		if(!Captcha.val()){
			LayerOpen('请填写图片验证码');
			return false;
		}
		$.post(url.sendcode, {
      		area_code: mcode,
			phone: PhoneNumber.val(),
			captcha_code: Captcha.val(),
			from: 'register',
			key_str: 'captcha-'+PhoneNumber.val()
		}, function (data) {
			$('#captcha').hide();
			CaptchaStatus = false;
			Captcha.val('');
			if (data.status_code == "200") {
				LayerOpen(data.message);
				$(".fr").css({ "background": "#ddd", "color": "#444" }).html("60秒重新发送").unbind("click");
				var num = 59;
				var time = setInterval(function () {
					$(".fr").html(num-- + "s后重新发送");
					if (num < 0) {
						clearTimeout(time);
						$(".fr").css({ "background": "#e4393c", "color": "#fff" }).html("重新发送");
						$(".fr").bind("click", getCode);
					}
				}, 1000);
			} else {
				LayerOpen(data.message);
			}
		})

	} else {
		if (!email(PhoneNumber.val())) {
			LayerOpen('请输入正确的邮箱');
			return false;
		}
		if (!CaptchaStatus) {
			$.get(url.captcha, {
				phone: PhoneNumber.val(),
			}, function (data) {
				if (data.status_code == "200") {
					LayerOpen('请填写图片验证码');
					$('#captcha img').attr('src',data.data.captcha_image_content);
					$('#captcha').show();
					CaptchaStatus = true;
				} else {
					LayerOpen(data.message);
				}
			})
			return
		}
		if(!Captcha.val()){
			LayerOpen('请填写图片验证码');
			return false;
		}
        
		$.post(url.sendmail, {
			email: PhoneNumber.val(),
			from: 'register',
		}, function (data) {
			$('#captcha').hide();
			CaptchaStatus = false;
			Captcha.val('');
			if (data.status_code == "200") {
				LayerOpen(data.message);
				$(".fr").css({ "background": "#ddd", "color": "#444" }).html("60秒重新发送").unbind("click");
				var num = 59;
				var time = setInterval(function () {
					$(".fr").html(num-- + "s后重新发送");
					if (num < 0) {
						clearTimeout(time);
						$(".fr").css({ "background": "#e4393c", "color": "#fff" }).html("重新发送");
						$(".fr").bind("click", getCode);
					}
				}, 1000);
			} else {
				LayerOpen(data.message);
			}
		})
	}
}


register.click(function () {
	if (registerType == "mobile") {
		if (!PhoneNumber.val()) {
			LayerOpen('请输入正确的手机号');
			return false;
		}
	} else {
		if (!email(PhoneNumber.val())) {
			LayerOpen('请输入正确的邮箱');
			return false;
		}
	}
	if (!mobicode.val()) {
		LayerOpen('请填写验证码');
		return;
	}
	if (!pwd(PassWord.val())) {
		LayerOpen("登录密码格式有误");
		return;
	}
	if (ConfirmPW.val() != PassWord.val()) {
		LayerOpen("两次输入的密码不一致");
		return;
	}
	var _data = {
		username: PhoneNumber.val(),  //手机号/邮箱
		v_code: mobicode.val(),  //验证码
		password: $.md5($.md5($.md5($.md5($.md5(PassWord.val()))))),//密码
		password_confirmation: $.md5($.md5($.md5($.md5($.md5(ConfirmPW.val()))))),//密码
		invite_code: $('#tjuser').val() ,  //推荐码
		area_code: mcode,
	};
	// if (registerType == 'email') {
	// 	delete _data['area_code'];
	// 	delete _data['phone'];
	// 	_data['email'] = PhoneNumber.val()
	// }

	$.post(url.regshuju, _data, function (data) {
		if (data.status_code == "200") {
			LayerOpen("注册成功");
			setTimeout(function () {
				window.location.href = contentURL;
			}, 1500)
		} else {
			LayerOpen(data.message);
		}
	})
})


function LayerOpen(conentta) {
	layer.open({
		style: 'border:none; background-color:#404040; color:#fff;',
		content: conentta,
		time: 1,
	});
};

//验证邮箱
function email(em) {
	var reg = /^\w+@[0-9A-Za-z]+\.[a-z]{2,4}$/;
	return reg.test(em);
};

//验证码格式验证
function code(val) {
	var reg = /^\d{6}$/
	return reg.test(val)
}
// 密码验证
function pwd(val) {
	var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/
	return reg.test(val);
}

/*url地址参数获取*/
function GetQueryString(name) {
	var reg = new RegExp("(^|&|)" + name + "=([^&?]*)(&|$|)", "i");
	var r = window.location.search.substr(1).match(reg);
	var context = "";
	if (r != null)
		context = r[2];
	reg = null;
	r = null;
	return context == null || context == "" || context == "undefined" ? "" : context;
};

// 下拉菜单
$(".mcode-box").on('click', function () {
	$(".dropDown").toggle()
})
$(".dropDown").on('click', 'li', function (event) {
	mcode = $(this).attr('data-mcode')
	$(".dropDown").toggle()
	$(".mcode-text").text('+' + mcode)
})
// tab切换
$(".tab").on('click', function (event) {
	var type = event.target.dataset.type
	tab(type)
})

function tab(type) {
	var mobileBox = $("#mobile")
	var emailBox = $("#email")
	var mcodeBox = $(".mcode-box")
	var block = $(".block")
	PhoneNumber.val("")
	mobicode.val("")
	PassWord.val(""),
		ConfirmPW.val("")
	registerType = type
	if (type == 'mobile') {
		// tab样式
		mobileBox.toggleClass('active')
		emailBox.removeClass('active')
		block.removeClass('goRight')
		// 改变文字
		PhoneNumber.attr('placeholder', '请输入手机号')
		mcodeBox.show()
		PhoneNumber.css('paddingLeft', '75px')
	} else {
		// tab样式
		emailBox.toggleClass('active')
		mobileBox.removeClass('active')
		block.addClass('goRight')
		// 改变文字
		PhoneNumber.attr('placeholder', '请输入邮箱')
		// 移除区号
		mcodeBox.hide()
		PhoneNumber.css('paddingLeft', '15px')
	}
}
tab(registerType)
